package cn.hospital.controller;

import cn.hospital.entity.*;
import cn.hospital.service.PatientInformationSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("patientSignsAnemia")
public class patientSignsAnemiaController {
    @Autowired
    private PatientInformationSheetService patientInformationSheetService;

    /**
     * 随访进度管理，查询单个患者信息
     * @param patientId
     * @return
     */
    @RequestMapping("/patientSignsAnemiaFoDiagnosis")
    public String patientSignsAnemiaFoDiagnosis(Integer patientId, Model model, HttpSession session) {
        List<PatientInformationSheet> patientInformationSheets = patientInformationSheetService.selectPatientSignsAnemiaFoDiagnosis(patientId);
        model.addAttribute("pisList", patientInformationSheets);
        return "page/grouping/patientsWithDetails";
    }

    /**
     * 患者详细页面中，保存按钮
     *
     * @param patientInformationSheet
     * @param record
     * @param anemia
     * @return
     */
    @RequestMapping("/updatePatientInformationSheet")
    @ResponseBody
    public Map<String, Object> updatePatientInformationSheet(Integer patientIds, PatientInformationSheet
            patientInformationSheet, String dateOfBirths, SignsOfBlood record, Anemia anemia) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        patientInformationSheet.setDateOfBirth(sdf.parse(dateOfBirths));
        patientInformationSheet.setPatientId(patientIds);
        record.setPatientId(patientIds);
        anemia.setPatientId(patientIds);
        Map<String, Object> map = new HashMap<>();
        Integer count = patientInformationSheetService.savePatientInformationSheet(
                patientInformationSheet, record, anemia);
        map.put("count",count);
        return map;
    }

    /**
     * 根据科室查询模板
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/selectTemplatemDepartment")
    public String selectTemplatemDepartment(HttpSession session, Model model) {
        List<Template> templateList = patientInformationSheetService.selectTemplatem(session);
        model.addAttribute("templateList", templateList);
        return "page/PatientInformation/addpatient";
    }

    /**
     * 添加病人模板信息
     *
     * @return
     */
    @RequestMapping("/ITPIS")
    public String insertTemplatemPatientInformationSheet(Ward ward, String provinceCode1, String cityCode1,
                                                         String countyCode1, String gates1, String provinceCode2, String cityCode2,
                                                         String countyCode2, String gates2,
                                                         PatientInformationSheet patientInformationSheet,
                                                         Score score, Certificate certificate, PatientsFamily patientsFamily,
                                                         Diagnosis diagnosis, OperationInformationSheet operationInformationSheet,
                                                         BloodTransfusionRecord bloodTransfusionRecord,
                                                         MedicalInformation medicalInformation, SignsOfBlood signsOfBlood,
                                                         Anemia anemia, ChestImaging chestImaging, MedicalHistory medicalHistory,
                                                         PulmonaryMetastasis pulmonaryMetastasis, AnesthesiaInformation anesthesiaInformation,
                                                         LiverMetastasis liverMetastasis,
                                                         OtherTransfer otherTransfer,
                                                         LymphNodeMetastasis lymphNodeMetastasis,
                                                         BrainMetastases brainMetastases,
                                                         BoneMetastases boneMetastases,
                                                         TumorMarker tumorMarker,
                                                         String dateOfBirthStr, String admissionTimeStr,
                                                         String dischargeTimeStr, String treatmentStartTimeStr,
                                                         String treatmentEndTimeStr, String treatmentFailureTimeStr,
                                                         String doctorsAdviceTimeStr, String dateOfSurgeryStr,
                                                         String timeOfOperationStr, String operationEndTimeStr,
                                                         String preparationTimeForAnesthesiaStr, String onsetTimeOfAnesthesiaStr,
                                                         String endTimeOfAnesthesiaStr,HttpSession session) {
        if (preparationTimeForAnesthesiaStr != null) {
            anesthesiaInformation.setPreparationTimeForAnesthesia(time(preparationTimeForAnesthesiaStr));
        }
        if (onsetTimeOfAnesthesiaStr != null) {
            anesthesiaInformation.setOnsetTimeOfAnesthesia(time(onsetTimeOfAnesthesiaStr));
        }
        if (endTimeOfAnesthesiaStr != null) {
            anesthesiaInformation.setEndTimeOfAnesthesia(time(endTimeOfAnesthesiaStr));
        }
        if (dateOfBirthStr != null) {
            patientInformationSheet.setDateOfBirth(time(dateOfBirthStr));
        }
        if (admissionTimeStr != null) {
            diagnosis.setAdmissionTime(time(admissionTimeStr));
        }
        if (dischargeTimeStr != null) {
            diagnosis.setDischargeTime(time(dischargeTimeStr));
        }
        if (treatmentStartTimeStr != null) {
            medicalHistory.setTreatmentStartTime(time(treatmentStartTimeStr));
        }
        if (treatmentEndTimeStr != null) {
            medicalHistory.setTreatmentEndTime(time(treatmentEndTimeStr));
        }
        if (treatmentFailureTimeStr != null) {
            medicalHistory.setTreatmentFailureTime(time(treatmentFailureTimeStr));
        }
        if (doctorsAdviceTimeStr != null) {
            medicalInformation.setDoctorsAdviceTime(time(doctorsAdviceTimeStr));
        }
        if (dateOfSurgeryStr != null) {
            operationInformationSheet.setDateOfSurgery(time(dateOfSurgeryStr));
        }
        if (timeOfOperationStr != null) {
            operationInformationSheet.setTimeOfOperation(time(timeOfOperationStr));
        }
        if (operationEndTimeStr != null) {
            operationInformationSheet.setOperationEndTime(time(operationEndTimeStr));
        }
        Integer count = patientInformationSheetService.insertTemplatemPatientInformationSheet(
                ward, provinceCode1, cityCode1,
                countyCode1, gates1, provinceCode2, cityCode2,
                countyCode2, gates2, patientInformationSheet,
                score, certificate, patientsFamily,
                diagnosis, operationInformationSheet,
                bloodTransfusionRecord, medicalInformation, signsOfBlood,
                anemia, chestImaging, medicalHistory,
                pulmonaryMetastasis, anesthesiaInformation,
                liverMetastasis, otherTransfer,
                lymphNodeMetastasis, brainMetastases,
                boneMetastases, tumorMarker,session);
        return "redirect:/patientSignsAnemia/selectTemplatemDepartment";
    }


    /**
     * 根据条件，查询患者数据
     */
    @RequestMapping("/findBy")
    @ResponseBody
    public Map<String,Object> findBy(String name, String gender, String startDateBirth,
                                     String endDateBirth, String startDate,
                                     String endDate,String pageNoStr, String pageSizeStr){
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        /*条件map集合*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        map.put("patient_name", name);
        map.put("gender", gender);
        map.put("startDateBirth", startDateBirth);
        map.put("endDateBirth", endDateBirth);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        Map<String,Object> mapList = new HashMap<>();
        mapList.put("countList",patientInformationSheetService.selectByMapCount(map));
        mapList.put("pisList",patientInformationSheetService.selectByMap(map));
        return mapList;
    }

    /**
     * 时间转换
     *
     * @param time
     * @return
     */
    public Date time(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

}
